<?php

/**
 * contact_person actions.
 *
 * @package    tmdb
 * @subpackage contact_person
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class contact_personActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->contact_person_list = Doctrine::getTable('Person')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->contact_person = Doctrine::getTable('Person')->find(array($request->getParameter('id')));
		$this->client_id = ($request->getParameter('client_id'));
    $this->forward404Unless($this->contact_person);
  }

  public function executeNew(sfWebRequest $request)
  {
		$person = new Person();
		$person->setclient_id($request->getParameter('client_id'));
		$person->setType('contact');
		$person->setAuthor($this->getUser()->getGuardUser()->getId());
		$this->client_id = ($request->getParameter('client_id'));
    $this->form = new ContactPersonForm($person);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));
		$person = new Person();
		$client_id = ($request->getParameter('client_id'));
		$person->setType('contact');
		$person->setAuthor($this->getUser()->getGuardUser()->getId());
    $this->form = new ContactPersonForm($person);

    $this->processForm($request, $this->form);
		$this->client_id = ($request->getParameter('client_id'));
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($contact_person = Doctrine::getTable('Person')->find(array($request->getParameter('id'))), sprintf('Object contact_person does not exist (%s).', array($request->getParameter('id'))));
		$this->client_id = ($contact_person->getclient_id());
    $this->form = new ContactPersonForm($contact_person);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($contact_person = Doctrine::getTable('Person')->find(array($request->getParameter('id'))), sprintf('Object contact_person does not exist (%s).', array($request->getParameter('id'))));
    $this->form = new ContactPersonForm($contact_person);
		$this->client_id = ($contact_person->getclient_id());
    $this->processForm($request, $this->form, $this->client_id);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($contact_person = Doctrine::getTable('Person')->find(array($request->getParameter('id'))), sprintf('Object contact_person does not exist (%s).', array($request->getParameter('id'))));
		$this->client_id = ($contact_person->getclient_id());
    $contact_person->delete();

    $this->redirect('mc/show?id='.$client_id);
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $contact_person = $form->save();
			$client_id = $contact_person->getclient_id();
			$this->client_id = $client_id;
      $this->redirect('contact_person/edit?id='.$contact_person['id'].'&client_id='.$client_id);
    }
  }
}
